﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace DAL.DFactory
{
    public class DSqlServer: DatabaseObject
    {
        #region Tạo đối tượng thực thi lệnh Command
        public override IDbCommand CreateCommand()
        {
            IDbCommand cmd = new SqlCommand();
            return cmd;
        }

        public override IDbCommand CreateCommand(string cmdText)
        {
            IDbCommand cmd = CreateCommand();
            cmd.CommandText = cmdText;

            return cmd;
        }

        public override IDbCommand CreateCommand(string cmdText, IDbConnection cn)
        {
            IDbCommand cmd = CreateCommand(cmdText);
            cmd.Connection = (SqlConnection)cn;

            return cmd;
        }

        public override IDbCommand CreateCommand(string cmdText, IDbConnection cn, IDbTransaction trans)
        {
            IDbCommand cmd = CreateCommand(cmdText, cn);
            cmd.Transaction = (SqlTransaction)trans;

            return cmd;
        }
        #endregion

        #region Tạo đối tượng kết nối Connection
        public override IDbConnection CreateConnection()
        {
            IDbConnection conn = new SqlConnection();
            return conn;
        }

        public override IDbConnection CreateConnection(string cnString)
        {
            IDbConnection conn = CreateConnection();
            conn.ConnectionString = cnString;

            return conn;
        }
        #endregion

        #region Tạo đố tượng chuyển dữ liệu trung gian DataAdapter
        public override IDbDataAdapter CreateDataAdapter()
        {
            IDbDataAdapter adapter = new SqlDataAdapter();
            return adapter;
        }

        public override IDbDataAdapter CreateDataAdapter(IDbCommand selectCmd)
        {
            IDbDataAdapter adapter = CreateDataAdapter();
            adapter.SelectCommand = selectCmd;

            return adapter;
        }

        public override IDbDataAdapter CreateDataAdapter(string selectCmdText, string selectCnString)
        {
            IDbDataAdapter adapter = CreateDataAdapter();
            adapter.SelectCommand = CreateCommand(selectCmdText, CreateConnection(selectCnString));

            return adapter;
        }

        public override IDbDataAdapter CreateDataAdapter(string selectCmdText, IDbConnection selectCn)
        {
            IDbDataAdapter adapter = CreateDataAdapter();
            adapter.SelectCommand = CreateCommand(selectCmdText, (SqlConnection)selectCn);

            return adapter;
        }
        #endregion

        #region Tạo đối tượng đọc dữ liệu DataReader
        public override IDataReader CreateDataReader(IDbCommand dbCmd)
        {
            IDataReader reader = dbCmd.ExecuteReader();
            return reader;
        }

        public override IDataReader CreateDataReader(IDbCommand dbCmd, CommandBehavior dbCmdBehavior)
        {
            IDataReader reader = dbCmd.ExecuteReader(dbCmdBehavior);
            return reader;
        }
        #endregion
    }
}
